package c.x.jy.service.service.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.dao.dao.BusinessDao;
import c.x.jy.dao.entity.BusinessEntity;
import c.x.jy.service.service.BusinessService;
import c.x.jy.service.utils.MemberUtil;

@CacheConfig(cacheNames = { "business" })
@Service("businessService")
public class BusinessServiceImpl extends ServiceImpl<BusinessDao, BusinessEntity> implements BusinessService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<BusinessEntity> query= 	 new QueryWrapper<BusinessEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<BusinessEntity> page = this.page(
                new Query<BusinessEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

	@Cacheable(key = " 'Business:'+ methodName +':' +#p0", unless = "#result == null")
	@Override
	public BusinessEntity selectBusinessByCode(String code) {
		if (code == null) return null;
		QueryWrapper<BusinessEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(BusinessEntity::getCode, code);
		return this.baseMapper.selectOne(queryWrapper);

	}

	@Cacheable(key = " 'Business:'+ methodName +':' +#p0", unless = "#result == null")
	@Override
	public BusinessEntity selectBusinessById(Long id) {
		if (id == null) return null;
		QueryWrapper<BusinessEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(BusinessEntity::getId,id);
		return this.baseMapper.selectOne(queryWrapper);

	}
	
	/**
	 * 仅供微信端使用
	 */
	@Override
	public BusinessEntity getCurrBusiness() {

		return selectBusinessByCode(MemberUtil.get().getBusinessCode());
	}

}
